;==============================================================================;
;; AilenOS bootstrap
;; tools : nasm
;==============================================================================;
	ORG	0
	BITS	16

	JMP 0x07c0:MAIN
;------------------------------------------------------------------------------;
; Data
N_READ	DW 0000h
N_KERNL	DW 0000h
S_INTRO	DB "AilenOS has been booted just.", 0
S_ERROR	DB "Error - on reading process.", 0
S_FLPST	DB "AilenOS will read floppy disk.", 0
S_FLPEN DB "AilenOS has finished reading the floppy.",0
S_DOT	DB ".", 0
;------------------------------------------------------------------------------;
; Function Parameta
P_TRACK	DB 0
P_HEAD	DB 0
P_SEC	DB 0
;------------------------------------------------------------------------------;
; Functions
PRINT:	PUSHA
LP_P:	MOV AH, BYTE[DS:SI]
	MOV BYTE[ES:DI], AH
	INC SI
	INC DI
	MOV BYTE[ES:DI], 0x07
	INC DI
	TEST AH,AH
	JNZ LP_P
P_END:	POPA
	RET
;------------------------------------------------------------------------------;
CLEAN:	PUSHA
	MOV AX, 0x0720
LP_C:	MOV WORD[ES:DI], AX
	ADD DI,2
	CMP DI, 80*25*2
	JNE LP_C
C_END:	POPA
	RET
;------------------------------------------------------------------------------;
; find where to read.
NEXTREAD:
	PUSH AX
	MOV AL, BYTE[P_SEC]
	CMP AL, 18
	JB CH_SEC
	MOV BYTE[P_SEC], 1
	MOV AL, BYTE[P_HEAD]
	CMP AL, 1
	JB CH_HEAD
	MOV BYTE[P_HEAD], 0
	INC BYTE[P_TRACK]
	POP AX
	RET
CH_HEAD:INC BYTE[P_HEAD]
	POP AX
	RET
CH_SEC: INC BYTE[P_SEC]
	POP AX
	RET
;------------------------------------------------------------------------------;
FLP:	PUSHA

	MOV DI, 80*2  ; the number of the characters in one line
	MOV SI, S_FLPST
	CALL PRINT

	; it will be never changed.
	MOV DI, WORD[N_READ]
	INC DI		; including IPL
	PUSH ES
	MOV AX, 0x0800
	MOV ES, AX
	MOV BX, 0x0000	; loads on ES:BX = 0x8000
	MOV AH, 0x02	; read disk
	MOV AL, 1	; 1 sector(s)
	MOV DL, 0	; drive 0(floppy A:)

	MOV BYTE[P_TRACK], 0	; cylinder 0
	MOV BYTE[P_HEAD],  0	; head 0(front)
	MOV BYTE[P_SEC],   0	; sector 2

FLP_LOOP:
	CALL NEXTREAD
	MOV CH, BYTE[P_TRACK]
	MOV CL, BYTE[P_SEC]
	MOV DH, BYTE[P_HEAD]

	PUSHA
	INT 0x13 ;13	; disk interrupt -- oh, no! bug!! why did I write '13' instead of '0x13'!!!
	JC  ERROR
	POPA

	ADD BX, 0x0200
	DEC DI
	JNZ FLP_LOOP

	POP ES
	MOV DI, 80*2*2
	MOV SI, S_FLPEN
	CALL PRINT
	POPA
	RET
;------------------------------------------------------------------------------;
; Main
MAIN:	;Initialize Registers
	CLI
	MOV AX, CS
	MOV DS, AX
	MOV FS, AX
	MOV GS, AX
	MOV SS, AX
	MOV AX, 0xB800
	MOV ES, AX
	XOR DI, DI

	;Clean the dislay
	CALL CLEAN

	MOV SI, S_INTRO
	CALL PRINT

	MOV AX, 10
	CALL FLP

	JMP 0x0820:0000
;------------------------------------------------------------------------------;
; Error!
ERROR:
	POP ES
	XOR AX, AX
	MOV SI, S_ERROR
	CALL PRINT

	CLI
	HLT
;------------------------------------------------------------------------------;
TIMES	510-($-$$)	DB 0
	DW 0xAA55
;==============================================================================;
; End of Code.